﻿@model IEnumerable<PointTask>
@{

    ViewData["Title"] = new string[] { "积分任务" };
    TempData["SidebarType"] = "PointTask";
    var user = UserContext.CurrentUser;
    var taskCompleted = ViewData.Get<IEnumerable<PointTaskRecord>>("taskCompleted", null);
}

<div class="row jh-space-setup">
    @Html.Partial("_Sidebar")
    <div class="col-xs-10">
        <div class="jh-space-right jh-padding-white">
            <ul class="nav nav-tabs tn-mb-20" role="tablist" id="myTab">
                <li role="presentation" class="active">
                    <a href="#taskList" data-href="#taskList" name="maintabnav"  role="button" data-toggle="tab">
                        任务列表
                    </a>
                </li>
                <li role="presentation">
                    <a href="#performTast" data-href="#performTast" name="maintabnav" role="button" data-toggle="tab">
                        已完成的任务
                    </a>
                </li>
            </ul>
            <div class="tab-content" id="_ListPointTasks">
                @Html.Action("_ListPointTasks", "UserSpace")
            </div>
        </div>
    </div>
</div>

<script type="text/javascript">
    require(['jquery', 'tnlayer'], function ($, tnlayer) {
        //弹出详情页
        $(document).on("click", ".taskdetail", function () {
            var taskId = $(this).data("taskid");
            var taskName = $(this).data("taskname");
            var $taskinfo = $(this).parents(".taskinfogo").find(".taskinfo");
            $(".taskinfo").removeClass("taskinfochecked");
            $taskinfo.addClass("taskinfochecked");
            var url = "@CachedUrlHelper.Action("_PointTaskDetail", "UserSpace")?t=" + (new Date).getTime();
            //页面层
            $.get(url, { taskId: taskId }, function (data) {
                layer.closeAll();
                layer.open({
                    type: 1,
                    title: taskName,
                    //skin: 'layui-layer-rim', //加上边框
                    area: ["500px", "500px"], //宽高
                    content: data,
                    scrollbar: true
                });
            });
        }).on("click", "#newtask", function () {
            //领取任务
            var taskurl = $("#taskinfo").data("taskurl");
            var url = "@CachedUrlHelper.Action("CreateTasktoUser", "UserSpace")";
            $.post(url, { taskId: $("#taskinfo").data("taskid") }, function (data) {
                if (data.MessageType == 1) {
                    layer.closeAll();
                    layer.msg(data.MessageContent, { icon: 1, time: 1000 }, function () {
                        if (taskurl == "")
                            getTaskList();
                        else
                            window.location.href = taskurl;
                    });
                }
                else {
                    layer.msg(data.MessageContent, { icon: 2 });
                }
            });
        }).on("click", ".newtask", function () {
            //领取任务
            var $this = $(this);
            var taskurl = $this.data("taskurl");
            var url = "@CachedUrlHelper.Action("CreateTasktoUser", "UserSpace")";
            $.post(url, { taskId: $this.data("taskid") }, function (data) {
                if (data.MessageType == 1) {
                    layer.closeAll();
                    layer.msg(data.MessageContent, { icon: 1, time: 1000 }, function () {
                        if (taskurl == "")
                            getTaskList();
                        else
                            window.location.href = taskurl;
                    });
                }
                else {
                    layer.msg(data.MessageContent, { icon: 2 });
                }
            });
        }).on("click", "#abandonedtask", function () {
            //放弃任务
            layer.confirm("确定放弃么？", {
                btn: ['确认', '取消'],//按钮
                scrollbar: false
            }, function () {
                var url = "@CachedUrlHelper.Action("AbandonedTask", "UserSpace")";
                $.post(url, { recordId: $("#taskinfo").data("recordid") }, function (data) {
                    if (data.MessageType == 1) {
                        layer.closeAll();
                        layer.msg(data.MessageContent, { icon: 1, time: 1000 });
                        getTaskList();
                    }
                    else {
                        layer.msg(data.MessageContent, { icon: 2 });
                    }
                });
            });
        }).on("click", "#taskgo", function () {
            //点击人工审核任务弹出任务框
            var taskName = $("#taskinfo").data("taskname");
            var url = "@CachedUrlHelper.Action("_PointTaskEdit", "UserSpace")?t=" + (new Date).getTime();
            //页面层
            $.get(url, { recordId: $("#taskinfo").data("recordid") }, function (data) {
                layer.open({
                    type: 1,
                    title: taskName,
                    //skin: 'layui-layer-rim', //加上边框
                    area: ["400px", "500px"], //宽高
                    content: data,
                    scrollbar: true
                });
            });
        }).on("click", ".taskgo", function () {
            layer.closeAll();
            //点击人工审核任务弹出任务框
            var $taskinfo = $(this).parents(".taskinfogo").find(".taskinfo");
            $(".taskinfo").removeClass("taskinfochecked");
            $taskinfo.addClass("taskinfochecked");
            var taskName = $taskinfo.data("taskname");
            var url = "@CachedUrlHelper.Action("_PointTaskEdit", "UserSpace")?t=" + (new Date).getTime();
            //页面层
            $.get(url, { recordId: $taskinfo.data("recordid") }, function (data) {
                layer.open({
                    type: 1,
                    title: taskName,
                    //skin: 'layui-layer-rim', //加上边框
                    area: ["400px", "500px"], //宽高
                    content: data,
                    scrollbar: true
                });
            });
        }).on("click", "#taskcancel", function () {
            //取消按钮
            layer.closeAll();
        }).on("click", "#tasksubmit", function () {
            //人工审核任务提交任务
            var content = [];
            var isEmpty = false;
            $(".content").each(function () {
                if ($(this).val() == "")
                {
                    isEmpty = true;
                    return;
                }
                content.push($(this).val().replace(",", "，"));
            });
            if (isEmpty)
            {
                layer.msg("任务提交内容不能为空！", { icon: 2 });
                return false;
            }
            var url = '@CachedUrlHelper.Action("PointTaskSubmit", "UserSpace")';
            $.post(url, { recordId: $(".taskinfochecked").data("recordid"), content: content }, function (data) {
                if (data.MessageType == 1) {
                    layer.closeAll();
                    layer.msg(data.MessageContent, { icon: 1, time: 1000 });
                    getTaskList();
                }
                else {
                    layer.msg(data.MessageContent, { icon: 2 });
                }
            });
        }).on("click", "#deadlineover", function () {
            //截止日期已过
            layer.msg("任务截止时间已过，不能领取任务！", { icon: 2 });
        });

        function getTaskList() {
            var url = '@CachedUrlHelper.Action("_ListPointTasks", "UserSpace")?t=' + (new Date).getTime();
            $.get(url, function (data) {
                $("#_ListPointTasks").html(data);
            })
        }
    });

</script>
